{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('12.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>标签节点</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a2\">a2</a>\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CSS中的内容\n",
    "- id是必须加#，#id\n",
    "- class是必须加. ，.class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<div id=\"div1\">\n",
       " <p id=\"p\">p1</p>\n",
       " <a class=\"ac\" href=\"a1\">a1</a>\n",
       " </div>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('#div1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<a class=\"ac\" href=\"a1\">a1</a>\n",
       "</div>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select_one('#div1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<a class=\"ac\" href=\"a1\">a1</a>, <a class=\"ac\" href=\"a2\">a2</a>]\n",
      "<a class=\"ac\" href=\"a1\">a1</a>\n"
     ]
    }
   ],
   "source": [
    "print(sp.select('.ac'))\n",
    "print(sp.select_one('.ac'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "## 如果获取的内容不存在\n",
    "print(sp.select('.ko'))\n",
    "print(sp.select_one('.acb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('13.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "<meta charset=\"utf-8\"/>\n",
       "<meta content=\"width=device-width, initial-scale=1.0\" name=\"viewport\"/>\n",
       "<meta content=\"ie=edge\" http-equiv=\"X-UA-Compatible\"/>\n",
       "<title>CSS选择器语法</title>\n",
       "</head>\n",
       "<body>\n",
       "<div id=\"div1\">\n",
       "<p id=\"p\">p1</p>\n",
       "<span class=\"ac span de\" id=\"sp3\">span</span>\n",
       "</div>\n",
       "<div id=\"div2\">\n",
       "<p id=\"p\">p2</p>\n",
       "<a class=\"ac\" href=\"a2\">a2</a>\n",
       "<span class=\"ac\" id=\"sp2\">sp</span>\n",
       "</div>\n",
       "</body>\n",
       "</html>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"ac\" href=\"a2\">a2</a>]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('a.ac')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<span class=\"ac span de\" id=\"sp3\">span</span>,\n",
       " <span class=\"ac\" id=\"sp2\">sp</span>]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('span.ac')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<span class=\"ac span de\" id=\"sp3\">span</span>,\n",
       " <a class=\"ac\" href=\"a2\">a2</a>,\n",
       " <span class=\"ac\" id=\"sp2\">sp</span>]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('.ac')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<p id=\"p\">p1</p>, <p id=\"p\">p2</p>]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.select('p#p')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
